<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增教材软著')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content" >
    <form class="form-horizontal m" id="form-jiaocairuanzhu-add"  enctype="multipart/form-data">
        <!--            <div class="form-group">    -->
        <!--                <label class="col-sm-3 control-label is-required">学院：</label>-->
        <!--                <div class="col-sm-8">-->
        <!--                    <select name="xueyuan" class="form-control m-b" th:with="type=${@dict.getType('sys_dept')}" required>-->
        <!--                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
        <!--                    </select>-->
        <!--                </div>-->
        <!--            </div>-->
        <!--            <div class="form-group">    -->
        <!--                <label class="col-sm-3 control-label is-required">姓名：</label>-->
        <!--                <div class="col-sm-8">-->
        <!--                    <input name="xingming" class="form-control" type="text" required>-->
        <!--                </div>-->
        <!--            </div>-->
        <!--            <input type="hidden" name="currentUser" th:value="${currentUserName}" />-->


        <div class="form-group"  style="display: none;">
            <label class="col-sm-3 control-label is-required" >姓名：</label>
            <div class="col-sm-8">
                <select name="userId" class="form-control m-b" >
                    <!--                            <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${user.flag}"></option>-->
                    <option th:each="user : ${sysUsers}" th:text="${user.userName}" th:value="${user.userId}" th:selected="${user.flag}"></option>
                </select>
            </div>
        </div>



        <div class="form-group">
            <div class="col-sm-8">
                <input name="jifen" class="form-control" type="hidden" value="0">
            </div>

            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">教材名称：</label>
                <div class="col-sm-8">
                    <input name="mingcheng" class="form-control" type="text" required>
                </div>
            </div>
<!--            <div class="col-sm-6">-->
<!--                <label class="col-sm-4 control-label is-required">类型：</label>-->
<!--                <div class="col-sm-8">-->
<!--                    <select name="leixing" class="form-control" th:with="type=${@dict.getType('sys_jiaocairuanzhu_leibie')}" required>-->
<!--                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->


            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">ISBN：</label>
                <div class="col-sm-8">
                    <input name="isbn"
                           class="form-control"
                           type="text"
                           required
                           inputmode="numeric"
                           pattern="\d{13}"
                           minlength="13"
                           maxlength="13"
                           title="请输入13位数字ISBN"
                           oninput="this.value=this.value.replace(/[^0-9]/g,'')">
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">出版时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="chubanshijian" class="form-control" placeholder="yyyy-MM-dd" type="text" required autocomplete="off">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">出版社：</label>
                <div class="col-sm-8">
                    <input name="chubanshe" class="form-control" type="text" required>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">本校是否用书：</label>
                <div class="col-sm-8" th:with="type=${@dict.getType('sys_yes_no')}">
                    <label th:each="dict : ${type}" class="check-box">
                        <input name="benxiaoshifoyongshu" type="radio" th:value="${dict.dictValue}" th:text="${dict.dictLabel}" required>
                    </label>
                </div>
            </div>
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">分类：</label>
                <div class="col-sm-8">
                    <select name="fenlei" class="form-control" th:with="type=${@dict.getType('sys_jiaocairuanzhu_fenlei')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">排名：</label>
                <div class="col-sm-8">
                    <select name="paiming" class="form-control" th:with="type=${@dict.getType('sys_jiaocairuanzhu_fzr')}" required>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
        </div>

        <div class="form-group row">
            <div class="col-sm-6">
                <label class="col-sm-4 control-label is-required">教材文件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="wenjian">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="wenjian" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <!-- 可以在这里添加另一个输入框，如果没有则留空 -->
            </div>
        </div>

    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/jiaocairuanzhu"
    $("#form-jiaocairuanzhu-add").validate({
        focusCleanup: true
    });


    function submitHandler() {
        if ($.validate.form()) {
            // 检查文件是否上传
            if ($("input[name='wenjian']").val() == "") {
                return $.modal.alertError("请上传教材文件");
            }

            // 获取教材名称和负责人级别
            const mingcheng = $("input[name='mingcheng']").val();
            const paiming = $("select[name='paiming']").val();

            // 发起异步请求验证是否存在相同记录
            $.ajax({
                url: prefix + "/checkDuplicate",
                type: "POST",
                data: { mingcheng: mingcheng, paiming: paiming },
                async: false,
                success: function (res) {
                    if (res.code !== 0) {
                        // 如果不是 code == 0，则提示错误并阻止提交
                        $.modal.alertWarning(res.msg);
                    } else {
                        // 否则正常提交
                        $.operate.saveTab(prefix + "/add", $('#form-jiaocairuanzhu-add').serialize());
                    }
                },
                error: function () {
                    $.modal.alertError("系统异常，请稍后再试");
                }
            });

        }
    }

    $("input[name='chubanshijian']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });



    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload/jcrz',
        initialPreviewAsData: true,
        maxFileCount: 1,
        autoReplace: true,
        allowedFileExtensions: ["pdf"],
        slugCallback: function(filename) {
            if ($("input[name='mingcheng']").val()===""){
                $(".amountfile-upload").fileinput('clear');
                $.modal.alertError("请先输入名称");
                return false;
            }
            // 自定义文件名生成规则
            fileExtension = filename.split('.').pop();
            const year = new Date().getFullYear();
            const newName = year +"-"+ $("input[name='mingcheng']").val() +"-"+ "教材软著"+ "." + fileExtension;
            return newName;
        },

    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })
</script>
</body>
</html>